/**
 * 角色管理
 * @author yuan_rz
 * @date 2019/01/04 09:55
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            treeData: []
        },
        table_where = {},
        cols = [[
            {field: 'ROW_ID', width: 60, title: '序号', fixed: 'left', hide: true}
            , {field: 'NAME', width: 150, title: '角色名称', fixed: 'left', sort: true, search: 'default'}
            , {field: 'IS_ENABLE', width: 60, title: '是否可用', filterDatas: app.datas_('sys_role_is_enable')}
            , {field: 'TYPE', width: 60, title: '角色类型', filterDatas: app.datas_('role')}
            , {field: 'BIZ_CODE', width: 80, title: '扩展码'}
            , {field: 'REMARK', width: 80, title: '备注'}
            , {
                width: 60,
                title: '',
                toolbar: '#script-rolesetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        moduleNodeData,
        moduleNodeDataByRole,
        roleSettingAction,
        role_edit_openOpts = {
            title: '角色编辑',
            area: ['600px', '264px']
        }
    ;

    var role = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'role',
                _searchs: [{
                    id: 'role', tableId: 'role_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'role_edit', tpl: 'system/role_edit'
                }, {
                    dom: 'role_modules', tpl: 'system/role_modules'
                }, {
                    dom: 'role_cmps', tpl: 'system/role_cmps'
                }, {
                    dom: 'role_users', tpl: 'system/role_users'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'role_table'),
                    url: g_ctxPath + 'sys/role/getRolesList',
                    exportsUrl: g_ctxPath + 'sys/role/exportRoles',
                    exportsName: '角色数据',
                    title: '角色信息表',
                    method: 'post',
                    id: 'role_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-rolesetting-action').innerHTML).render({}, function (string) {
                    roleSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(role_table)', function (obj) {

                    layer.tips(roleSettingAction, obj.td, {
                        tips: [3, '']
                        , time: 5000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '1':
                                        self._openTplFull({
                                            tag: 'role_edit',
                                            tagData: {
                                                obj: obj,
                                                detail: false
                                            },
                                            index: 0,
                                            openOpts: role_edit_openOpts
                                        });
                                        break;
                                    case '2':
                                        app.confirm({
                                            msg: '是否删除选择的角色？',
                                            ok: function () {
                                                self.deleterole(obj);
                                            }
                                        });
                                        break;
                                    case '3':
                                        self._openTplFull({
                                            tag: 'role_modules',
                                            tagData: {
                                                roleId: obj.data.ID,
                                                // treeData: moduleNodeData
                                                treeData: moduleNodeDataByRole
                                            },
                                            index: 1,
                                            openOpts: {
                                                title: '模块授权',
                                                area: ['560px', '560px']
                                            }
                                        });
                                        break;
                                    case '4':
                                        self._openTplFull({
                                            tag: 'role_cmps',
                                            tagData: {
                                                roleId: obj.data.ID,
                                                // treeData: moduleNodeData
                                                treeData: moduleNodeDataByRole
                                            },
                                            index: 2,
                                            openOpts: {
                                                title: '组件授权'
                                            }
                                        });
                                        break;
                                    case '5':
                                        self._openTplFull({
                                            tag: 'role_users',
                                            tagData: obj,
                                            index: 3,
                                            openOpts: {
                                                title: '分配用户',
                                                area: ['900px', '560px']
                                            }
                                        });
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(role_table)', function (obj) {
                    self._openTplFull({
                        tag: 'role_edit',
                        tagData: {
                            obj: obj,
                            detail: true
                        },
                        index: 0,
                        openOpts: role_edit_openOpts
                    });
                });

                //监听事件
                table.on('toolbar(role_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'role_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: role_edit_openOpts
                            });
                            break;
                        case 'delete':
                            layer.msg('删除');
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'role_table', where: table_where});
        },
        deleterole: function (obj) {
            app.ajax({
                xurl: 'sys/role/delete',
                xdata: obj.data,
                loading: true,
                okmsg: '角色信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        initTree: function () {
            app.ajax({
                xurl: 'sys/module/getModulesTree',
                cb: function (result) {
                    if (result.ok) {
                        moduleNodeData = result.data.data;
                        vueData.treeData.pushArray(moduleNodeData);
                    }
                }
            });
        },
        initModuleTree: function () {
            app.ajax({
                xurl: 'sys/module/getModulesTreeByRole',
                cb: function (result) {
                    if (result.ok) {
                        moduleNodeDataByRole = result.data.data;
                        //vueData.treeData.pushArray(moduleNodeData);
                    }
                }
            });
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'role',
                    methods: {}
                }]
            });

            self.initTable();
            self.initTree();
            self.initModuleTree();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new role();

});